{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加四行信息添加五行信息添加六行信息添加六行信息添加六行信息添加八行信息\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\npath = r\"C:\\\\Zhunda_HTML\\\\python\\x0cile_operation\\\\data.txt\"\\nwith open(path, \"r\", encoding=\"UTF-8\") as file:\\n    line = file.readlines()\\n    print(line)\\n\\n'"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# io操作\n",
    "import io\n",
    "# 文件路径\n",
    "# open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)\n",
    "path = r\"C:\\Zhunda_HTML\\python\\file_operation\\data.txt\"\n",
    "try:\n",
    "    file = open(path, \"r\", encoding=\"UTF-8\")\n",
    "    line = file.readline()\n",
    "except Exception as exc:\n",
    "    print(exc)\n",
    "finally:\n",
    "    file.close()\n",
    "print(line)\n",
    "\n",
    "'''\n",
    "path = r\"C:\\Zhunda_HTML\\python\\file_operation\\data.txt\"\n",
    "with open(path, \"r\", encoding=\"UTF-8\") as file:\n",
    "    line = file.readlines()\n",
    "    print(line)\n",
    "\n",
    "'''\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "division by zero\n",
      "finally执行\n",
      "启动后续代码块\n"
     ]
    }
   ],
   "source": [
    "# 异常处理\n",
    "# 不进行异常处理，程序终止运行\n",
    "try:\n",
    "    23+2/0\n",
    "# except (ZeroDivisionError,NameError,TypeError):\n",
    "#     print(ZeroDivisionError)\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "finally:\n",
    "    print(\"finally执行\")\n",
    "print(\"启动后续代码块\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "path = r\"C:\\Zhunda_HTML\\python\\file_operation\\data.txt\"\n",
    "try:\n",
    "    file = open(path, \"a+\", encoding=\"UTF-8\")\n",
    "    file.write(r\"添加八行信息\") # 将字符串写入文件，返回的是写入的字符长度。\n",
    "    result = file.readline()\n",
    "    print(result)\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "finally:\n",
    "    file.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'id': '001', 'name': '回锅肉'}, {'id': '002', 'name': '鱼香肉丝'}, {'id': '003', 'name': '青椒肉丝'}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "menu = [{\"id\":\"001\",\"name\":\"回锅肉\"},\n",
    "        {\"id\":\"002\",\"name\":\"鱼香肉丝\"},\n",
    "        {\"id\":\"003\",\"name\":\"青椒肉丝\"}]\n",
    "path = r\"C:\\Zhunda_HTML\\python\\file_operation\\data.txt\"\n",
    "with open(path, \"w+\", encoding=\"UTF-8\") as file:\n",
    "    json.dump(menu, file, ensure_ascii=False)\n",
    "\n",
    "with open(path, \"r\", encoding=\"UTF-8\") as file:\n",
    "    \n",
    "    '''\n",
    "    result = file.read()\n",
    "    result = str(result).replace(\"\\'\", \"\\\"\")\n",
    "    '''\n",
    "\n",
    "    result = json.load(file)\n",
    "    \n",
    "    print(result)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
